package com.xyht.medicine_management_system.modules.medicineSource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicine;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineMapper;
import com.xyht.medicine_management_system.modules.medicineSource.entity.MmsMedicineSource;
import com.xyht.medicine_management_system.modules.medicineSource.entity.MmsMedicineSourceMedicineRelation;
import com.xyht.medicine_management_system.modules.medicineSource.entity.req.MmsMedicineSourceMedicineRelationReq;
import com.xyht.medicine_management_system.modules.medicineSource.entity.resp.MmsMedicineSourceAndMedicineRelationResp;
import com.xyht.medicine_management_system.modules.medicineSource.mapper.MmsMedicineSourceMapper;
import com.xyht.medicine_management_system.modules.medicineSource.mapper.MmsMedicineSourceMedicineRelationMapper;
import com.xyht.medicine_management_system.modules.medicineSource.service.MmsMedicineSourceMedicineRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.medicine_management_system.modules.medicineSource.util.MedicineSourceUtil;
import org.springframework.stereotype.Service;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.medicine.constant.MedicineIsOnConstant.MEDICINE_IS_OFF;
import static com.xyht.medicine_management_system.modules.medicineSource.constant.IdTypeConstant.ID_TYPE_MEDICINE;
import static com.xyht.medicine_management_system.modules.medicineSource.constant.IdTypeConstant.ID_TYPE_SOURCE;
import static com.xyht.medicine_management_system.modules.medicineSource.constant.IsDisplayConstant.IS_DISPLAY_FALSE;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 药知源与商品关联表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-12-05
 */
@Service
public class MmsMedicineSourceMedicineRelationServiceImpl extends ServiceImpl<MmsMedicineSourceMedicineRelationMapper, MmsMedicineSourceMedicineRelation> implements MmsMedicineSourceMedicineRelationService {
    @Resource
    private MmsMedicineSourceMedicineRelationMapper relationMapper;
    @Resource
    private MedicineSourceUtil medicineSourceUtil;

    @Override
    public ResponseResult addRelation(MmsMedicineSourceMedicineRelationReq relationReq) {
        if (isNullOrEmpty(relationReq.getSourceId(), relationReq.getMedicineId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsMedicineSourceMedicineRelation relation = relationMapper.selectOne(new LambdaQueryWrapper<MmsMedicineSourceMedicineRelation>()
                .eq(MmsMedicineSourceMedicineRelation::getMedicineId, relationReq.getMedicineId())
                .eq(MmsMedicineSourceMedicineRelation::getSourceId, relationReq.getSourceId()));
        if (relation != null) {
            return CommonResult.failed(CommonCodeEnum.MEDICINE_SOURCE_RELATION_EXIST);
        }

        MmsMedicineSourceMedicineRelation addRelation = new MmsMedicineSourceMedicineRelation();
        addRelation.setMedicineId(relationReq.getMedicineId());
        addRelation.setSourceId(relationReq.getSourceId());
        relationMapper.insert(addRelation);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteRelation(MmsMedicineSourceMedicineRelationReq relationReq) {
        if (isNullOrEmpty(relationReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        relationMapper.deleteById(relationReq.getId());

        return CommonResult.success();
    }

    @Override
    public ResponseResult getRelationList(String id, Integer idType) {
        if (isNullOrEmpty(id)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if (!idType.equals(ID_TYPE_MEDICINE) && !idType.equals(ID_TYPE_SOURCE)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<MmsMedicineSourceAndMedicineRelationResp> list = medicineSourceUtil.getRelationList(id,idType);
        return CommonResult.success(list);
    }
}
